package com.timothy.junk.common.parse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;
import com.timothy.junk.R;
import com.timothy.gfund.bean.Repo;

import android.content.Context;
import android.content.res.XmlResourceParser;

public class RepoParser {

	 public static List<Repo> getRepoList(Context context) {
	        List<Repo> rList = new ArrayList<Repo>();
	        XmlResourceParser xrp = context.getResources().getXml(R.xml.repos);
	        try {
	            // ��û�е�XML�ĵ��Ľ�β��
	            Repo repo = null;
	            while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
	                if (xrp.getEventType() == XmlResourceParser.START_TAG) {
	                    String tagName = xrp.getName();
	                    if (tagName.equals("repo")) {
	                        repo = new Repo();
	                    }
	                    if(tagName.equals("index") && repo !=null){
	                        repo.setIndex(Integer.valueOf(xrp.nextText()));
	                    }
	                    if(tagName.equals("type") && repo !=null){
	                        repo.setType(Integer.valueOf(xrp.nextText()));
	                    }
	                    if(tagName.equals("code") && repo !=null){
	                        repo.setCode(xrp.nextText());
	                    }
	                    if(tagName.equals("name") && repo !=null){
	                        repo.setName(xrp.nextText());
	                    }
	                }
	                if(xrp.getEventType() == XmlResourceParser.END_TAG){
	                    String tagName = xrp.getName();
	                    if (tagName.equals("repo")) {
	                        rList.add(repo);
	                    }
	                }
	                xrp.next();
	            }
	        } catch (XmlPullParserException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return rList;
	    }

	    public static List<String> getRepoCodeList(Context context) {
	        List<String> rList = new ArrayList<String>();
	        XmlResourceParser xrp = context.getResources().getXml(R.xml.repos);
	        try {
	            // ��û�е�XML�ĵ��Ľ�β��
	            while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
	                if (xrp.getEventType() == XmlResourceParser.START_TAG) {
	                    String tagName = xrp.getName();
	                    if(tagName.equals("code")){
	                        rList.add(xrp.nextText().toString());
	                    }
	                }
	                xrp.next();
	            }
	        } catch (XmlPullParserException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return rList;
	    }

	    public static List<String> getSHRepoCodeList(Context context) {
	        List<String> rList = new ArrayList<String>();
	        XmlResourceParser xrp = context.getResources().getXml(R.xml.funds);
	        try {
	            // ��û�е�XML�ĵ��Ľ�β��
	            while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
	                if (xrp.getEventType() == XmlResourceParser.START_TAG) {
	                    String tagName = xrp.getName();
	                    String tagValue = xrp.nextText().toString();
	                    if(tagName.equals("code") && tagValue.startsWith("sh")){
	                        rList.add(tagValue);
	                    }
	                }
	                xrp.next();
	            }
	        } catch (XmlPullParserException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return rList;
	    }

	    public static List<String> getSZRepoCodeList(Context context) {
	        List<String> rList = new ArrayList<String>();
	        XmlResourceParser xrp = context.getResources().getXml(R.xml.funds);
	        try {
	            // ��û�е�XML�ĵ��Ľ�β��
	            while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
	                if (xrp.getEventType() == XmlResourceParser.START_TAG) {
	                    String tagName = xrp.getName();
	                    String tagValue = xrp.nextText().toString();
	                    if(tagName.equals("code") && tagValue.startsWith("sz")){
	                        rList.add(tagValue);
	                    }
	                }
	                xrp.next();
	            }
	        } catch (XmlPullParserException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return rList;
	    }
}
